Skip to content

fix(session-replay-browser): don't record or upload under an expired Analytics session - #2217

Open
bravecod wants to merge 1 commit into
mainfrom
josephjin/src-501-session-replay-records-under-an-expired
Open

bravecod wants to merge 1 commit into
mainfrom
josephjin/src-501-session-replay-records-under-an-expired

Conversation

@bravecod

@bravecod bravecod commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes SRC-501: 0s replays that show up even with Minimum Session Duration set (reported by Almosafer).

Cause. After a long break, the Analytics SDK restores the old session id on page load. It only starts a new session when its next event fires. Session Replay recorded under that expired session in the gap. The session's min duration clock started hours earlier, so the gate passed at once and a ~60ms snapshot got uploaded as a 0s replay. The same happens without a reload, when a tab is left open (or frozen by a mobile browser) past the session timeout.

Fix. The plugin passes the Analytics session timeout and a live lastEventTime getter to Session Replay (analyticsSessionExpiry, marked @internal). A session counts as expired when now - lastEventTime > sessionTimeout, the same rule Analytics uses (isNewSession).

  • Page load: initialize() does not start recording under an expired session. Recording starts when setSessionId brings the new session. The session id is still passed to init, so interaction (heatmap) capture is set up as before.
  • Same tab: if the session expires while recording, the min duration clock restarts once (and is saved). When Analytics starts a new session, the leftover is below the gate and gets dropped by the existing per-session verdict. Recording does not stop, so if no Analytics event comes (user only scrolls), the session passes again and the content is kept.

Unchanged: standalone @amplitude/session-replay-browser users (they manage their own session ids, and SR can't know their session rules), plugin users with customSessionId, and sessions without a min duration threshold.

Known limits

  • With no Analytics event on page load, recording starts after the first event.
  • After a same-tab clock restart, if the user leaves within the threshold, up to that much content after expiry is dropped.

Testing

  • Unit tests for both packages pass with 100% coverage (session-replay-browser 1227, plugin 75).
  • Browser check with the real Analytics SDK + plugin + SR (throwaway page and Playwright spec, not committed), min duration 3s, Analytics session timeout 30 min, clock moved forward 35 min. Counted SR uploads per session:
Scenario Before (main) After
Short visit, reload 35 min later, then an Analytics event expired session: 2 uploads, new session: 1 expired session: 0, new session: 2
Short visit, same tab comes back 35 min later, then an Analytics event expired session: 3 uploads, new session: 1 expired session: 0, new session: 1

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: No

🤖 Generated with Claude Code


Note

Medium Risk
Changes session start/recording and min-duration upload gating for plugin-integrated replay; behavior shifts around page load after long idle periods and same-tab expiry, though scoped to plugin users without customSessionId.

Overview
Fixes 0-second Session Replay uploads when Analytics has already expired a session but Session Replay still records under the restored session id (page reload or idle tab).

The plugin now passes an internal analyticsSessionExpiry hook (sessionTimeout + live lastEventTime) into Session Replay, unless customSessionId is used. Session Replay uses the same rule as Analytics (now - lastEventTime > sessionTimeout) to:

  • Skip starting recording on init when the restored session is expired; recording resumes after setSessionId with a new session.
  • Restart the minimum session duration clock once when expiry is detected while recording, so stale start times cannot immediately pass the gate and upload near-empty replays.

Standalone SDK users are unchanged (no expiry hook). Unit tests cover wiring and expiry/min-duration behavior.

Reviewed by Cursor Bugbot for commit f1587d0. Bugbot is set up for automated code reviews on this repo. Configure here.

…Analytics session

After a long break, the Analytics SDK restores the old session id and only
starts a new session when its next event fires. Session Replay recorded under
that expired session in the gap. Its min duration clock had started long ago,
so the gate passed at once and a near-empty replay was uploaded (0s replays).

The plugin now passes the Analytics session timeout and a live lastEventTime
getter to Session Replay (analyticsSessionExpiry). With it:
- On init, SR does not start recording under an expired session. It starts
  when setSessionId brings the new session.
- If the session expires while recording (same tab, no reload), the min
  duration clock restarts once, so the leftover is dropped when Analytics
  starts a new session. Recording continues, so content is kept if no
  Analytics event comes.

Standalone SDK users and plugin users with customSessionId are unchanged.

SRC-501

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bravecod
bravecod requested a review from a team as a code owner December 1, 2026 22:42
@linear-code

linear-code Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

SRC-501

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

size-limit report 📦

Path Size
packages/analytics-browser/lib/scripts/amplitude-min.js.gz 68.9 KB (0%)
packages/session-replay-browser/lib/scripts/session-replay-browser-min.js.gz 141.54 KB (+0.19% 🔺)
packages/unified/lib/scripts/amplitude-min.umd.js.gz 228.63 KB (+0.12% 🔺)
@amplitude/element-selector (gzipped esm) 3.48 KB (0%)
@amplitude/analytics-browser/lite (gzipped CDN bundle) 21.44 KB (0%)

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

Session Replay Browser E2E Results

passed  179 passed
flaky  1 flaky

Details

stats  180 tests across 20 suites
duration  7 minutes, 4 seconds
commit  f1587d0

Flaky tests

chromium › e2e/idb.spec.ts › IDB transaction abort handling › getSequencesToSend cursor abort: AbortError logged at debug not warn

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant